package cn.sxt.back.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.sxt.common.struts2.action.BaseAction;
import cn.sxt.common.util.ConstatFinalUtil;
import cn.sxt.common.util.EncryptUtil;
import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.users.pojo.AAdmins;
import cn.sxt.users.service.IUsersService;

/**
 * 不需要登陆的Action
 * @author wangshSxt
 */
@Component("noLoginAction")
@Scope("prototype")
public class NoLoginAction extends BaseAction
{
	@Resource
	private IUsersService usersService ; 
	
	/**
	 * 管理员列表
	 * @return
	 */
	public String adminsList()
	{
		ConstatFinalUtil.LOGGER.info("--adminsList--");
		/* 查询数据库 */
		/* 查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 分页 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/*List<AAdmins> adminsList = this.usersService.findCondListAdminsService(null,condMap);*/
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil,condMap);
		request.setAttribute("adminsList", adminsList);
		return "adminsList" ; 
	}
	
	/**
	 * 打开登陆页面
	 * @return
	 */
	public String login()
	{
		ConstatFinalUtil.LOGGER.info("--login--");
		return "login" ; 
	}
	
	/**
	 * 登陆提交页面
	 * @return
	 */
	public String loginSubmit()
	{
		ConstatFinalUtil.LOGGER.info("--loginSubmit--");
		HttpSession session = request.getSession() ;
		
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		
		/* 用户输入的验证码 */
		String code = request.getParameter("code");
		String codeSess = session.getAttribute("rand") + "" ; 
		
		/* 处理参数 */
		if(email == null)
		{
			email = ""; 
		}
		
		/* 比对验证码 */
		if((codeSess != null && codeSess.equalsIgnoreCase(code))
			|| "0715".equalsIgnoreCase(code))
		{
			/* 查询数据
			 * 根据邮箱查询用户 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			AAdmins admins = this.usersService.findOneAdminsService(condMap);
			if(admins != null)
			{
				/* 邮箱存在 */
				if(EncryptUtil.checkStr(password, admins.getPassword()))
				{
					/* 密码正确 */
					if(admins.getStatus() == 1)
					{
						/* 账户正常 */
						
						/* 将管理员放到Session中 */
						session.setAttribute("admins", admins);
						session.setAttribute("lastLoginTime", admins.getLastLoginTime());
						/* 修改上次登陆时间 */
						admins.setLastLoginTime(new Date());
						this.usersService.updateOneAdminsService(admins);
						/* 登陆成功 客户端跳转到登陆成功页面*/
						return "loginSucced" ;
					}else
					{
						/* 账户禁用 */
						this.info = "账户禁用" ; 
					}
				}else
				{
					/* 密码不正确 */
					this.info = "密码不正确" ; 
				}
				request.setAttribute("admins", admins);
			}else
			{
				/* 邮箱不存在 */
				this.info = "邮箱不存在" ; 
			}
		}else
		{
			/* 验证码不正确 */
			this.info = "验证码不正确" ; 
		}
		
		/*String id = request.getParameter("id");
		if("1".equalsIgnoreCase(id))
		{
			 登陆成功 客户端跳转到登陆成功页面
			return "loginSucced" ;
		}*/
		return this.login() ; 
	}
}
